package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ClassName: SearchReceiver
 * Package: com.atguigu.tingshu.search.receiver
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/7/3 Thursday 21:47
 * @Version 1.0
 */
@Slf4j
@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

    /**
     * 监听上架专辑队列
     *
     * @param albumId
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true", type = "direct"),
            key = MqConst.ROUTING_ALBUM_UPPER
    ))
    @SneakyThrows
    public void albumUpper(Long albumId, Message message, Channel channel) {
        if (albumId != null) {
            log.info("监听到上架专辑消息，albumId:{}", albumId);
            searchService.upperAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 监听下架专辑队列
     *
     * @param albumId
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true", type = "direct"),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    @SneakyThrows
    public void albumLower(Long albumId, Message message, Channel channel) {
        if (albumId != null) {
            log.info("监听到下架专辑消息，albumId:{}", albumId);
            searchService.lowerAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
